﻿using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D11;

namespace Engine.Graphics.Shaders
{
    public class ShaderBase
    {
       // public SharpDX.Direct3D11.Effect effect;
        public VertexShader vs;
        public PixelShader ps;
        public InputLayout inputLayout;
        public ShaderBytecode shaderByteCodeVS;
        public ShaderBytecode shaderByteCodePS;

        internal void LoadShaderASM(Device device, string fileName, string PixelShaderName, string VertexShaderName)
        {
            ShaderFlags shaderflags = ShaderFlags.OptimizationLevel3;
#if DEBUG
            //shaderflags = ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#else
            shaderflags = ShaderFlags.OptimizationLevel3;

#endif
            //ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Vertex_Shader_vs_main", "vs_3_0", shaderflags);
            //ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Vertex_Shader_vs_main", "vs_5_0", shaderflags);
            //ShaderBytecode shaderByteCode2 = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Pixel_Shader_ps_main", "ps_5_0", shaderflags);

            ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(fileName, "Textured", "fx_5_0", shaderflags);
            // ShaderBytecode shaderByteCode2 = ShaderBytecode.CompileFromFile(fileName, PixelShaderName, "ps_5_0", shaderflags);
           // Effect effect = new Effect(device, shaderByteCode.Data, EffectFlags.None);
            vs = new VertexShader(device, shaderByteCode.Data);
            //  ps = new PixelShader(device, shaderByteCode2.Data);
            //this.effect = new Effect(device, shaderByteCode.Data, EffectFlags.None);
            this.inputLayout = new InputLayout(device, shaderByteCode, Engine.Graphics.VertexStructures.MyOwnVertex.MyPositionNormalTextured.elements);
            device.ImmediateContext.InputAssembler.InputLayout = this.inputLayout;
        }

        internal void LoadShader(Device device, string fileName, string PixelShaderName, string VertexShaderName)
        {
            ShaderFlags shaderflags = ShaderFlags.OptimizationLevel3;
#if DEBUG
            shaderflags = ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#else
            shaderflags = ShaderFlags.OptimizationLevel3;

#endif
            //ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Vertex_Shader_vs_main", "vs_3_0", shaderflags);
            //ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Vertex_Shader_vs_main", "vs_5_0", shaderflags);
            //ShaderBytecode shaderByteCode2 = ShaderBytecode.CompileFromFile(fileName, "Effect_Group_1_Material00_Material00_Pixel_Shader_ps_main", "ps_5_0", shaderflags);

            shaderByteCodeVS = ShaderBytecode.CompileFromFile(fileName, VertexShaderName, "vs_4_0", shaderflags);
            shaderByteCodePS = ShaderBytecode.CompileFromFile(fileName, PixelShaderName, "ps_4_0", shaderflags);

            vs = new VertexShader(device, shaderByteCodeVS.Data);
            ps = new PixelShader(device, shaderByteCodePS.Data);
            //this.effect = new Effect(device, shaderByteCode.Data, EffectFlags.None);
            this.inputLayout = new InputLayout(device, shaderByteCodeVS, Engine.Graphics.VertexStructures.MyOwnVertex.MyPositionNormalTextured.elements);
            device.ImmediateContext.InputAssembler.InputLayout = this.inputLayout;
        }

        //internal void SetTexture(string valueName, ShaderResourceView texture)
        //{
        //    this.effect.GetVariableByName(valueName).AsShaderResource().SetResource(texture);
        //}

        //internal void SetColor4(string valueName, SharpDX.Color4 color)
        //{
        //    this.effect.GetVariableByName(valueName).AsVector().Set(color);
        //}

        //internal void SetVector4(string valueName, SharpDX.Vector4 vector)
        //{
        //    this.effect.GetVariableByName(valueName).AsVector().Set(vector);
        //}

        //internal void SetFloat(string valueName, float value)
        //{
        //    this.effect.GetVariableByName(valueName).AsVector().Set(value);
        //}

        //internal void SetMatrix3x2(string valueName, SharpDX.Matrix3x2 matrix3x2)
        //{
        //    this.effect.GetVariableBySemantic(valueName).AsMatrix().SetMatrix<Matrix3x2>(matrix3x2);
        //}

        //internal void SetMatrix(string valueName, Matrix matrix)
        //{
        //    this.effect.GetVariableBySemantic(valueName).AsMatrix().SetMatrix<Matrix>(matrix);
        //}

        //internal void SetBool(string valueName, bool value)
        //{
        //    this.effect.GetVariableBySemantic(valueName).AsVector().Set<bool>(value);
        //}

        //internal void Dispose()
        //{
        //    this.effect.Dispose();
        //}
    }
}